package com.boot.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.boot.sentinelHandler.BlockHandler;
import com.boot.sentinelHandler.FallBack1;
import com.boot.service.FeignService1;
import com.entities.CommonResult;
import com.entities.Payment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 05-08-19:28
 */
@RestController
public class Consumer88 {

//    private static final String SERVER_URL="http://sentinel-payment";

    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @Resource
    private FeignService1 feignService;

    @GetMapping("/consumer/{id}")
    @SentinelResource(value = "fallback",
            fallbackClass = FallBack1.class,fallback = "test02"
            ,blockHandlerClass = BlockHandler.class, blockHandler = "test03")
    public CommonResult<Payment> test01(@PathVariable("id") Integer id) {
        CommonResult commonResult = restTemplate.getForObject(serverURL + "/payment/nacos/" + id, CommonResult.class, id);
        if (id >= 4 || id <= 0) {
            throw new IllegalArgumentException("非法参数！");
        } else if (commonResult.getData() == null) {
            throw new NullPointerException("空指针异常！");
        }
        return commonResult;
    }

//    ********* openfeign 技术栈分割线 ********
    @GetMapping("consumer/feign/{id}")
    public CommonResult<Payment> test02(@PathVariable("id") Integer id){
        if (id > 4 ){
            throw new IllegalArgumentException("非法参数！");
        }
        return feignService.test01(id);
    }
}
